package is.billsharing.app;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class ReportPaymentActivity extends Activity implements
		AdapterView.OnItemSelectedListener {
	private static final String PREFS_NAME = "user";
	int uid;
	String[] friends;
	TextView tv, amount;
	CheckBox cb;
	String friendEmail;
	Button reportPayment;
	String paymentRecieved;
	String response;
	boolean checked;
	Double credit = null;
	Double debit = null;

	ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reportpayment);
		tv = (TextView) findViewById(R.id.textView1);
		amount = (TextView) findViewById(R.id.pay_amt);
		reportPayment = (Button) findViewById(R.id.report_pay);
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		uid = settings.getInt("uid", 0);
		final ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("uid", "" + uid + ""));
		try {
			String response = CustomHttpClient.executeHttpPost("Friends",postParameters);
			response = response.replace("[", "");
			response = response.replace("]", "");
			friends = response.split(",");
			tv.setText(response);
		} catch (Exception e) {
			e.printStackTrace();
		}

		final Spinner spin = (Spinner) findViewById(R.id.user_pay);
		spin.setOnItemSelectedListener(this);

		ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item, friends);

		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spin.setAdapter(aa);

		reportPayment.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ArrayList<NameValuePair> paymentPostParameters = new ArrayList<NameValuePair>();
				paymentPostParameters.add(new BasicNameValuePair("uid", ""+ uid +""));
			
				paymentPostParameters.add(new BasicNameValuePair("amount",amount.getText().toString()));
				paymentPostParameters.add(new BasicNameValuePair("friendEmail",	friendEmail));
				if (credit != null) {
					paymentPostParameters.add(new BasicNameValuePair("payment","recieved"));
				} else if (debit != null) {
					paymentPostParameters.add(new BasicNameValuePair("payment","made"));
				}

				try {
					response = CustomHttpClient.executeHttpPost("MakePayment", paymentPostParameters);
					tv.setText(response);

						Intent intent=new Intent(ReportPaymentActivity.this,TabsampleActivity.class);
            	    	startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});
	}

	void getBalance(CharSequence fndEmail) {
		friendEmail = (String) fndEmail;
		ArrayList<NameValuePair> friendPostParameters = new ArrayList<NameValuePair>();
		friendPostParameters.add(new BasicNameValuePair("uid", "" + uid + ""));
		//tv.setText(friendEmail);
		friendPostParameters.add(new BasicNameValuePair("friendEmail",friendEmail));
		try {
			response = CustomHttpClient.executeHttpPost("FriendBalance",friendPostParameters);

			JSONObject json = new JSONObject(response);
			if (json.has("credit")) {
				tv.setText(friendEmail + "owes you " + json.getDouble("credit")+ "$ report payment");
				credit = json.getDouble("credit");
				reportPayment.setVisibility(View.VISIBLE);
			} else if (json.has("debit")) {
				tv.setText("you owe " + friendEmail + " "+ json.getDouble("debit") + "$ report payment");
				debit = json.getDouble("debit");
				reportPayment.setVisibility(View.VISIBLE);
			} else {
				tv.setText("you are even with " + friendEmail);
				reportPayment.setVisibility(View.GONE);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		tv.setText((CharSequence) parent.getItemAtPosition(position));
		getBalance((CharSequence) parent.getSelectedItem());

	}

	public void onNothingSelected(AdapterView<?> parent) {
		tv.setText("");

	}


}
